package ru.batrdmi.svnplugin.logic;

public class StopWatch {
    private final String instanceName;
    private long totalTime;
    private long opStartTime;
    private String opName;

    public StopWatch(String instanceName) {
        this.instanceName = instanceName;
    }

    public void start(String opName) {
        opStartTime = getCurrentTime();
        this.opName = opName;
    }
    
    public void stop() {
        long timeInterval = getCurrentTime() - opStartTime;
        logElapsedTime(opName, timeInterval);
        totalTime += timeInterval;
    }

    public void logTotalTime() {
        logElapsedTime("Total time for " + instanceName, totalTime);
    }
    
    private static long getCurrentTime() {
        return System.nanoTime();
    }
    
    private static void logElapsedTime(String opName, long timeInterval) {
        System.out.println(opName + ": " + (timeInterval / 1000000) + " ms");
    }
}
